//
//  STSignInViewController.swift
//  iPhone
//
//  Created by Toj on 4/19/23.
//

import UIKit

typealias STSignInCompletionBlock = (()->Void)

// MARK: - 懒加载(属性), 变量, 初始化init
class STSignInViewController: STSignInBaseController {
    
    static let shared = STSignInViewController()
    
    private let viewModel = STSignInViewModel()
    
    /* STSignInView: 有账号叫SignIn */
    private lazy var signInView: STSignInView = {
        let view = STSignInView()
        view.delegate = self
        view.isHidden = true
        return view
    }()
    
    /* STSignInView: 无账号叫Login */
    private lazy var loginView: STLoginView = {
        let view = STLoginView()
        view.isHidden = true
        view.delegate = self
        return view
    }()
    
    private var _loginCompletion: STSignInCompletionBlock?
    private lazy var navController: UINavigationController = {
        let nav = STNavigationController(rootViewController: self)
        nav.view.backgroundColor = .clear
        nav.setNavigationBarHidden(true, animated: false)
        return nav
    }()
}

// MARK: - 生命周期
extension STSignInViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeNotification()
    }
}

// MARK: - 数据请求
private extension STSignInViewController {
    
    func loginRequest() {
        
    }
}

// MARK: - 点击事件
@objc private extension STSignInViewController {
    
    func registerFinished(notification: Notification) {
        removeFromFromParentController()
        _loginCompletion?()
        _loginCompletion = nil
    }
}

// MARK: - Public
extension STSignInViewController {
    
    func presentController(_ completion: STSignInCompletionBlock? = nil) {
        guard let rootViewController = UIApplication.shared.keyWindow?.rootViewController else { return }
        _loginCompletion = completion
        
//        navController.modalPresentationStyle = .custom
//        rootViewController.present(navController, animated: false)
        rootViewController.view.addSubview(navController.view)
        rootViewController.addChild(navController)

        let bounds = rootViewController.view.bounds
        navController.view.bounds = bounds
        
        showViews()
    }
    
    func dismiss(view: UIView) {
        view.isHidden = true
        guard signInView.isHidden && loginView.isHidden else {
            return
        }
        removeFromFromParentController()

        _loginCompletion = nil
    }
}

// MARK: - Private
private extension STSignInViewController {
    func showViews() {
        let isCache = arc4random_uniform(2) == 1
        if isCache {
            signInView.show()
            loginView.hide()
            return
        }
        loginView.show()
        signInView.hide()
    }
    
    func removeFromFromParentController() {
        navController.popToRootViewController(animated: false)
        navController.view.removeFromSuperview()
        navController.removeFromParent()
    }
}

// MARK: - 代理事件
extension STSignInViewController: STSignInViewDelegate {
   
    func signInViewDidTapClose(_ signInView: STSignInView) {
        dismiss(view: signInView)
    }
    
    func signInViewDidTapSignin(_ signInView: STSignInView) {
//        viewModel.signIn { [weak self] in
//            self?.dismiss(view: signInView)
//        }
        
        let vc = TestViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    func signInViewDidTapOthers(_ signInView: STSignInView) {
        loginView.show()
    }
}

// MARK: - 代理事件
extension STSignInViewController: STLoginViewDelegate {
    
    func loginViewDidTapClose(_ loginView: STLoginView) {
        dismiss(view: loginView)
    }
    
    func loginViewDidTapOthers(_ signInView: STLoginView) {
        
    }
    
    func loginViewDidTapLogin(_ signInView: STLoginView) {
        viewModel.loginIn { [weak self] in
            let isRegistered = arc4random_uniform(2) == 1
            if isRegistered {
                let vc = STSignInInputPasswordController()
                self?.navController.pushViewController(vc, animated: true)
                return
            }
            
            let vc = STRegisterViewController()
            self?.navController.pushViewController(vc, animated: true)
            return
            
            
            
//            self?.dismiss(view: loginView)
        }
    }
}

// MARK: - 初始化设置
private extension STSignInViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(signInView)
        view.addSubview(loginView)
    }
    
    func setupMakeLayoutSubviews() {
        
        signInView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(view)
        }
        
        loginView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {

    }
    
    func setupMakeNotification() {
        NotificationCenter.default.addObserver(self, selector: #selector(registerFinished(notification:)), name: STRegisterFinishView.registerFinishedNotification, object: nil)
    }
}
